<?php
/*
 * 会员管理控制器
 * class MemberController
 */
class MemberController{
    /*
     * 构造方法
     */
    private $data;
    public function __construct()
    {
        $this->data = include './data1.php';
    }

    /*
     * 登录
     */
    public function login(){
        if(IS_POST){
            $username = $_POST['username'];
            $password = md5($_POST['password']);
//            $vcode = strtoupper($_POST['vcode']);
            //判断验证码是否正确
            if (strtoupper($_POST['vcode']) != $_SESSION['vcode']){
                success('验证码错误','index.php?c=Member&a=login');
            }
            foreach ($this->data as $u){
                if ($u['username']==$username && $u['password']==$password){
                    $_SESSION['username'] = $username;
//                    p($_SESSION);
                    $re = isset($_POST['rememberme'])?$_POST['rememberme']:2;
                    if ($re == 1){
                        setcookie(session_name(),session_id(),time()+3600*24*7,'/');
                    }else{
                        setcookie(session_name(),session_id(),0,'/');
                    }
                    success('登录成功','index.php');
                }
            }
            success('登录失败','index.php?c=Member&a=login');
        }
        include 'View/login.php';
    }

    /*
     * 注册
     */
    public function reg(){
        if(IS_POST){
            //判断用户名是否重复
            $username = $_POST['username'];

            if (strtoupper($_POST['vcode']) != $_SESSION['vcode']){
                success('验证码错误','index.php?c=Member&a=reg');
            }
            if($_POST['password'] != $_POST['password2']){
                success('密码不一致','index.php?c=Member&a=reg');
                exit;
            }
            foreach ($this->data as $value){
                if ($value['username'] == $username){
                    success('用户名重复，请重新输入','index.php?c=Member&a=reg');
                    exit;
                }
            }

            //追加
            $this->data[]=array(
                'username'=>$username,
                'password'=>md5($_POST['password'])
            );
            //写入数据库
            file_put_contents('./data1.php',"<?php \r\nreturn " . var_export($this->data,true) . "\r\n?>");
            success('注册成功，请登录','index.php?c=Member&a=login');
        }
        include './View/reg.php';
    }
    //退出方法
    public function out(){
        session_unset();
        session_destroy();
        success('退出成功！','index.php');
    }

    //检查验证码
    public function checkCode(){
        $code = strtoupper($_POST['code']);
        if ($code == $_SESSION['vcode']){
            echo 1;
        }else{
            echo 0;
        }
    }

    //检查用户名
    public function checkUser(){
        $username = $_POST['u'];
        foreach ($this->data as $u){
            if ($username == null){
                echo 3;exit;
            }
            if ($u['username'] == $username){
                echo 1;
                exit;
            }
        }
        echo 0;
    }
}
